﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Text;
using EIM_Accounting.Common.DBUtility;//Please add references

namespace Accounting.DAL
{
    /// <summary>
    /// 数据访问类:WagesProvide
    /// </summary>
    public partial class WagesProvide
    {
        public WagesProvide()
        { }

        #region Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQLite.GetMaxID("ID", "WagesProvide");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from WagesProvide");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Accounting.Model.WagesProvide model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into WagesProvide(");
            strSql.Append("StaffID,ProvideMonth,BaseWage,Award,Punishment,AddWork,Remark,ProvideFee,ProvideDate)");
            strSql.Append(" values (");
            strSql.Append("@StaffID,@ProvideMonth,@BaseWage,@Award,@Punishment,@AddWork,@Remark,@ProvideFee,@ProvideDate)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@StaffID", DbType.Int32,8),
                    new SQLiteParameter("@ProvideMonth", DbType.String),
                    new SQLiteParameter("@BaseWage", DbType.Decimal,4),
                    new SQLiteParameter("@Award", DbType.Decimal,4),
                    new SQLiteParameter("@Punishment", DbType.Decimal,4),
                    new SQLiteParameter("@AddWork", DbType.Decimal,4),
                    new SQLiteParameter("@Remark", DbType.String),
                    new SQLiteParameter("@ProvideFee", DbType.Decimal,4),
                    new SQLiteParameter("@ProvideDate", DbType.String)};
            parameters[0].Value = model.StaffID;
            parameters[1].Value = model.ProvideMonth;
            parameters[2].Value = model.BaseWage;
            parameters[3].Value = model.Award;
            parameters[4].Value = model.Punishment;
            parameters[5].Value = model.AddWork;
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.ProvideFee;
            parameters[8].Value = model.ProvideDate;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Accounting.Model.WagesProvide model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update WagesProvide set ");
            strSql.Append("StaffID=@StaffID,");
            strSql.Append("ProvideMonth=@ProvideMonth,");
            strSql.Append("BaseWage=@BaseWage,");
            strSql.Append("Award=@Award,");
            strSql.Append("Punishment=@Punishment,");
            strSql.Append("AddWork=@AddWork,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("ProvideFee=@ProvideFee,");
            strSql.Append("ProvideDate=@ProvideDate");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@StaffID", DbType.Int32,8),
                    new SQLiteParameter("@ProvideMonth", DbType.String),
                    new SQLiteParameter("@BaseWage", DbType.Decimal,4),
                    new SQLiteParameter("@Award", DbType.Decimal,4),
                    new SQLiteParameter("@Punishment", DbType.Decimal,4),
                    new SQLiteParameter("@AddWork", DbType.Decimal,4),
                    new SQLiteParameter("@Remark", DbType.String),
                    new SQLiteParameter("@ProvideFee", DbType.Decimal,4),
                    new SQLiteParameter("@ProvideDate", DbType.String),
                    new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = model.StaffID;
            parameters[1].Value = model.ProvideMonth;
            parameters[2].Value = model.BaseWage;
            parameters[3].Value = model.Award;
            parameters[4].Value = model.Punishment;
            parameters[5].Value = model.AddWork;
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.ProvideFee;
            parameters[8].Value = model.ProvideDate;
            parameters[9].Value = model.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from WagesProvide ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from WagesProvide ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Accounting.Model.WagesProvide GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,ProvideMonth,BaseWage,Award,Punishment,AddWork,Remark,ProvideFee,ProvideDate from WagesProvide ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            Accounting.Model.WagesProvide model = new Accounting.Model.WagesProvide();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StaffID"] != null && ds.Tables[0].Rows[0]["StaffID"].ToString() != "")
                {
                    model.StaffID = int.Parse(ds.Tables[0].Rows[0]["StaffID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProvideMonth"] != null && ds.Tables[0].Rows[0]["ProvideMonth"].ToString() != "")
                {
                    model.ProvideMonth = ds.Tables[0].Rows[0]["ProvideMonth"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BaseWage"] != null && ds.Tables[0].Rows[0]["BaseWage"].ToString() != "")
                {
                    model.BaseWage = decimal.Parse(ds.Tables[0].Rows[0]["BaseWage"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Award"] != null && ds.Tables[0].Rows[0]["Award"].ToString() != "")
                {
                    model.Award = decimal.Parse(ds.Tables[0].Rows[0]["Award"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Punishment"] != null && ds.Tables[0].Rows[0]["Punishment"].ToString() != "")
                {
                    model.Punishment = decimal.Parse(ds.Tables[0].Rows[0]["Punishment"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddWork"] != null && ds.Tables[0].Rows[0]["AddWork"].ToString() != "")
                {
                    model.AddWork = decimal.Parse(ds.Tables[0].Rows[0]["AddWork"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ProvideFee"] != null && ds.Tables[0].Rows[0]["ProvideFee"].ToString() != "")
                {
                    model.ProvideFee = decimal.Parse(ds.Tables[0].Rows[0]["ProvideFee"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProvideDate"] != null && ds.Tables[0].Rows[0]["ProvideDate"].ToString() != "")
                {
                    model.ProvideDate = ds.Tables[0].Rows[0]["ProvideDate"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,ProvideMonth,BaseWage,Award,Punishment,AddWork,Remark,ProvideFee,ProvideDate ");
            strSql.Append(" FROM WagesProvide ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "WagesProvide";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method

        internal Model.WagesProvide GetModel(int staffID, string calcMonth)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,ProvideMonth,BaseWage,Award,Punishment,AddWork,Remark,ProvideFee,ProvideDate from WagesProvide ");
            strSql.Append(" where StaffID=@StaffID");
            strSql.Append(" and ProvideMonth=@ProvideMonth");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@StaffID", DbType.Int32,4),
                        new SQLiteParameter("@ProvideMonth", DbType.String)
};
            parameters[0].Value = staffID;
            parameters[1].Value = calcMonth;

            Accounting.Model.WagesProvide model = new Accounting.Model.WagesProvide();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StaffID"] != null && ds.Tables[0].Rows[0]["StaffID"].ToString() != "")
                {
                    model.StaffID = int.Parse(ds.Tables[0].Rows[0]["StaffID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProvideMonth"] != null && ds.Tables[0].Rows[0]["ProvideMonth"].ToString() != "")
                {
                    model.ProvideMonth = ds.Tables[0].Rows[0]["ProvideMonth"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BaseWage"] != null && ds.Tables[0].Rows[0]["BaseWage"].ToString() != "")
                {
                    model.BaseWage = decimal.Parse(ds.Tables[0].Rows[0]["BaseWage"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Award"] != null && ds.Tables[0].Rows[0]["Award"].ToString() != "")
                {
                    model.Award = decimal.Parse(ds.Tables[0].Rows[0]["Award"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Punishment"] != null && ds.Tables[0].Rows[0]["Punishment"].ToString() != "")
                {
                    model.Punishment = decimal.Parse(ds.Tables[0].Rows[0]["Punishment"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddWork"] != null && ds.Tables[0].Rows[0]["AddWork"].ToString() != "")
                {
                    model.AddWork = decimal.Parse(ds.Tables[0].Rows[0]["AddWork"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ProvideFee"] != null && ds.Tables[0].Rows[0]["ProvideFee"].ToString() != "")
                {
                    model.ProvideFee = decimal.Parse(ds.Tables[0].Rows[0]["ProvideFee"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProvideDate"] != null && ds.Tables[0].Rows[0]["ProvideDate"].ToString() != "")
                {
                    model.ProvideDate = ds.Tables[0].Rows[0]["ProvideDate"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}